package org.linlinjava.litemall.db.service;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.linlinjava.litemall.db.util.ThirdPartYigong;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

//志愿E+服务
@Service
public class VolunteerService {

    @Value("${yigong.appSecret}")
    private String appSecret;
    @Value("${yigong.appId}")
    private String appId;
    @Value("${yigong.hostUrl}")
    private String hostUrl;

    private static final String CREATE_USER = "thirdPart/createUser";
    private static final String PAY = "thirdPart/pay";
    private static final String CANCEL_ORDER = "thirdPart/cancelPay";//取消订单
    private static final String USER_INFO = "thirdPart/userInfo";
    private static final String FEEDBACK_INTEGRAL = "thirdPart/feedbackIntegral";//增加回馈积分
    private static final String DEL_FEEDBACK_INTEGRAL = "thirdPart/delFeedbackIntegral";//删除回馈积分

    public JsonObject createUser(String mobile){
        ThirdPartYigong tpy = getThirdPartYigong(mobile);
        String response = tpy.search(hostUrl + CREATE_USER);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        return jsonObject;
    }

    public JsonObject userInfo(String mobile){
        ThirdPartYigong tpy = getThirdPartYigong(mobile);
        String response = tpy.search(hostUrl + USER_INFO);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        return jsonObject;
    }

    public JsonObject pay(String mobile, BigDecimal integrals, String orderId, String remarks){
        ThirdPartYigong tpy = getThirdPartYigong(mobile);
        Map<String, String> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("remarks", remarks);
        params.put("integrals", integrals.toString());
        String response = tpy.post(hostUrl + PAY, params);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        return jsonObject;
    }



    public JsonObject cancelOrder(String mobile, String orderId, String remarks){
        ThirdPartYigong tpy = getThirdPartYigong(mobile);
        Map<String, String> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("integrals", "0");
        params.put("remarks", remarks);
        String response = tpy.post(hostUrl + CANCEL_ORDER, params);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        return jsonObject;
    }


    public JsonObject feedbackIntegral(String mobile, BigDecimal integral, String orderId, String remarks){
        ThirdPartYigong tpy = getThirdPartYigong(mobile);
        Map<String, String> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("integrals", integral.toString());
        params.put("remarks", remarks);
        String response = tpy.post(hostUrl + FEEDBACK_INTEGRAL, params);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        return jsonObject;
    }

    public JsonObject delFeedbackIntegral(String mobile, String orderId, BigDecimal integrals, String remarks){
        ThirdPartYigong tpy = getThirdPartYigong(mobile);
        Map<String, String> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("integrals", integrals.toString());
        params.put("remarks", remarks);
        String response = tpy.post(hostUrl + DEL_FEEDBACK_INTEGRAL, params);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        return jsonObject;
    }


    public ThirdPartYigong getThirdPartYigong(String mobile){
        ThirdPartYigong typ = new ThirdPartYigong();
        typ = typ.set(mobile, appId, appSecret);
        return typ;
    }


}
